package com.leng.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class MD5Util {
	/**
	 * 生成32位md5字符串
	 * @param source
	 * @return
	 */
	public static String md5(String source) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] bytes = md5.digest(source.getBytes());
			StringBuffer stringBuffer = new StringBuffer();
			for (byte b : bytes) {
				int bt = b & 0xff;
				if (bt < 16) {
					stringBuffer.append(0);
				}
				stringBuffer.append(Integer.toHexString(bt));
			}
			return stringBuffer.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 生成32位随机字符串
	 * @return
	 */
	public static String randomString(){
		UUID uuid=UUID.randomUUID();
        String str = uuid.toString(); 
        String uuidStr=str.replace("-", "");
        return uuidStr;
	}
	
	/**
	 * md5加盐
	 * @param source
	 * @param salt
	 * @return
	 */
	public static String md5(String source,String salt){
		return md5((source+salt));
	}
}
